package br.furb.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.model.ManagedBean;

import br.furb.jpa.dao.DAOFactory;
import br.furb.jpa.dao.MidiaDAO;
import br.furb.jpa.persistence.GenericPersistenceException;
import br.furb.jpa.persistence.OrderType;
import br.furb.models.Midia;

@ManagedBean(name = "midiaBean")
public class MidiaBean {

	private Midia midia;
	private List<Midia> midias;

	@PostConstruct
	public void init() {
		midia = new Midia();
		midias = new ArrayList<Midia>();
		System.out.println("new midia");
	}

	public String excluir(Midia midia) throws GenericPersistenceException {
		MidiaDAO midiaDao = (MidiaDAO) DAOFactory.getInstance().createDAO(
				Midia.class);
		midiaDao.delete(midia);
		return "sucesso";
	}

	public String salvar() throws GenericPersistenceException {
		MidiaDAO midiaDao = (MidiaDAO) DAOFactory.getInstance().createDAO(
				Midia.class);
		midiaDao.save(midia);
		refreshMidias();
		return "sucesso";
	}

	private void refreshMidias() {
		midias = new ArrayList<Midia>();
	}

	public List<Midia> getMidias() throws GenericPersistenceException {

		midias = new ArrayList<Midia>();
		MidiaDAO midiaDao = (MidiaDAO) DAOFactory.getInstance().createDAO(
				Midia.class);
		midias = midiaDao.search(OrderType.Asc);
		return midias;
	}

	public Midia getMidia() {
		return midia;
	}

	public void setDvd(Midia dvd) {
		this.midia = dvd;
	}

}
